﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace FirmaTransportowa
{
    public partial class AdminAddNewUser : Form
    {

        public AdminAddNewUser()
        {
            SqlConnection dbConnection = SqlConnectionFactory.getConnection();
            InitializeComponent();
        }

        User editedUser;
        bool editMode = false;
        public event Action eventClosed;

        public AdminAddNewUser(User user, bool edit)
        {
            editedUser = user;
            editMode = edit;
            InitializeComponent();
        }
       
        private void addButton_Click(object sender, EventArgs e)
        {

            bool fieldsFilledOut = true;
            if (loginBox.Text.Trim() == String.Empty)
            {
                loginError.Text = "Podaj login";
                loginError.ForeColor = Color.DarkRed;
                fieldsFilledOut = false;
            }

            if (passwordBox.Text.Trim() == String.Empty)
            {
                passwordError.Text = "Podaj hasło";
                passwordError.ForeColor = Color.DarkRed;
                fieldsFilledOut = false;
            }
            if (editMode == false)
            {
                if ((fieldsFilledOut) && (!User.checkIfRecordExists(loginBox.Text.Trim().ToString(), "User_")))
                {
                        try
                        {
                            User user = new User();
                            user.login = loginBox.Text.Trim().ToString();
                            user.password = passwordBox.Text.Trim().ToString();
                            user.worker = Worker.getById(int.Parse(workersDataGridView.Rows[workersDataGridView.CurrentRow.Index].Cells[0].Value.ToString()));
                            user.save();
                            string test = user.worker.id.ToString();
                            MessageBox.Show("Dodano użytkownika");
                            this.Close();
                        }
                       catch
                       {
                            MessageBox.Show("Wybierz pracownika");
                       }
                }
            }

            if (editMode == true)
            {
                if (fieldsFilledOut)
                {
                    {
                        try
                        {
                            editedUser.login = loginBox.Text.Trim().ToString();
                            editedUser.password = passwordBox.Text.Trim().ToString();
                            editedUser.worker = Worker.getById(int.Parse(workersDataGridView.Rows[workersDataGridView.CurrentRow.Index].Cells[0].Value.ToString()));
                            editedUser.save();
                            MessageBox.Show("Zmieniono użytkownika");
                        }
                        catch (NullReferenceException)
                        {
                            MessageBox.Show("Wybierz pracownika");
                        }
                    }
                }
            }
        }

        private void displayWorkers()
        {
            workersDataGridView.DataSource = Worker.getWorkers();
            workersDataGridView.Columns[0].HeaderText = "ID pracownika";
            workersDataGridView.Columns[1].HeaderText = "imię";
            workersDataGridView.Columns[2].HeaderText = "nazwisko";
            workersDataGridView.Columns[3].Visible = false;
            workersDataGridView.Columns[4].Visible = false;
            workersDataGridView.Columns[5].Visible = false;
        }

        private void AdminAddNewUser_Load(object sender, EventArgs e)
        {
            displayWorkers();
            if (editMode == true)
            {
                loginBox.Text = editedUser.login;
                passwordBox.Text = editedUser.password;
            }

        }

        private void AdminAddNewUser_HelpButtonClicked(object sender, CancelEventArgs e)
        {
            HelpUtils.showHelp(this, 21);
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.F1)
            {
                if (editMode == false)
                    HelpUtils.showHelp(this, 21);
                else
                    HelpUtils.showHelp(this, 22);
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
}
